<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>MINGHUE-签到功能</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script src="/js/jquery-1.12.4.min.js"></script>
</head>

<style>
    #container{
        height:400px;
        margin-top: 20px;
    }
    .info{
        width:26rem;
        display: none;
    }
    a{
        margin:20px;
    }
</style>

<body>
<!--日签到-->
<div class="layui-btn-container">
    <a href="/sign" target="_blank" class="layui-btn layui-btn-primary">今日签到</a>
    <a href="/sign/history" target="_blank" class="layui-btn layui-btn-primary">我的签到历史</a>
</div>
<!--时间段签到-->
<div class="layui-btn-container">
    <a href="/sign/lag" target="_blank" class="layui-btn layui-btn-primary">时段签到(18:00-20:00)</a>
    <a href="/sign/lag/history" target="_blank" class="layui-btn layui-btn-primary">我的签到历史</a>
</div>
<!--定位签到-->
<div class="layui-btn-container">
    <a href="javascript:;" id="geo_btn" class="layui-btn layui-btn-primary">定位签到</a>
    <a href="/sign/geo/history" target="_blank" class="layui-btn layui-btn-primary">我的签到历史</a>
    <a href="javascript:;" id="s_yester" class="layui-btn layui-btn-primary">查看昨日签到位置</a>
    <a href="javascript:;" onclick="clearMarker()" class="layui-btn layui-btn-primary">删除位置</a>
</div>
<!--高德定位信息-->
<div id='container'></div>
<fieldset class="layui-elem-field">
    <legend id="status"></legend>
    <div class="layui-field-box">
        <div class="info">
            <p id='result'></p><hr>
            <p >由于众多浏览器已不再支持非安全域的定位请求，为保位成功率和精度，请升级您的站点到HTTPS。</p>
        </div>
    </div>
</fieldset>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=你申请的key"></script>
<script type="text/javascript">
    var marker,map = new AMap.Map('container', {
        zoom: 16, //初始地图级别
        resizeEnable: true,
        showIndoorMap: false //关闭室内地图
    });
    var position;
    AMap.plugin('AMap.Geolocation', function() {
        var geolocation = new AMap.Geolocation({
            enableHighAccuracy: true,//是否使用高精度定位，默认:true
            timeout: 10000,          //超过10秒后停止定位，默认：5s
            buttonPosition:'RB',    //定位按钮的停靠位置
            buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
            zoomToAccuracy: true,   //定位成功后是否自动调整地图视野到定位点
        });
        map.addControl(geolocation);
        geolocation.getCurrentPosition(function(status,result){
            $('.info').css('display', 'block');
            if(status=='complete'){
                onComplete(result)
                position = result.position;
            }else{
                onError(result)
            }
        });
    });
    //解析定位结果
    function onComplete(data) {
        console.log(data.position);
        document.getElementById('status').innerHTML='定位成功'
        var str = [];
        str.push('定位结果：' + data.position);
        str.push('定位类别：' + data.location_type);
        if(data.accuracy){
            str.push('精度：' + data.accuracy + ' 米');
        }//如为IP精确定位结果则没有精度信息
        str.push('是否经过偏移：' + (data.isConverted ? '是' : '否'));
        document.getElementById('result').innerHTML = str.join('<br>');
    }
    //解析定位错误信息
    function onError(data) {
        document.getElementById('status').innerHTML='定位失败'
        document.getElementById('result').innerHTML = '失败原因排查信息:'+data.message;
    }
    //实例化点标记
    function addMarker(l,b) {
        marker = new AMap.Marker({//b代表经度,l代表纬度
            icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
            position: [l,b],//纬度、经度
            offset: new AMap.Pixel(-13, -30)
        });
        marker.setMap(map);
    }
    //清除 marker
    function clearMarker() {
        if (marker) {
            marker.setMap(null);
            marker = null;
        }
    }
    $(document).ready(function() {
        //定位签到ajax
        $('#geo_btn').click(function(event) {
            $.ajax({
                url: '/sign/geo',
                type: 'POST',
                dataType: 'json',
                data: {b:position.lat,l:position.lng}
            })
                .done(function(evs) {
                    console.log("success");
                    alert(evs.msg);
                })
                .fail(function() {
                    console.log("error");
                })
                .always(function() {
                    console.log("complete");
                });
        });
        $("#s_yester").click(function(event) {
            clearMarker();
            $.ajax({
                url: '/sign/geo/history/yesterday',
                type: 'GET',
                dataType: 'json',
            })
                .done(function(evs) {
                    console.log("success");
                    if(!$.isEmptyObject(evs))
                        addMarker(evs.l,evs.b);
                    else alert("昨日无签到数据！");
                })
                .fail(function() {
                    alert("昨日无签到数据！");
                })
                .always(function() {
                });

        });
    });
</script>
</body>
</html>